package com.dhz.common.solr.conf;

import com.dhz.common.solr.server.HttpSolrClientFactoryBean;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * 针对solr-client标签的处理类，构建实例
 *
 * @author Fyduan
 */
public class HttpSolrClientBeanDefinitionParser extends AbstractBeanDefinitionParser {

    @Override
    protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(HttpSolrClientFactoryBean.class);
        setSolrHome(element, builder);
        return getSourcedBeanDefinition(builder, element, parserContext);
    }

    private void setSolrHome(Element element, BeanDefinitionBuilder builder) {
        builder.addPropertyValue("url", element.getAttribute("url"));
        builder.addPropertyValue("timeout", element.getAttribute("timeout"));
        builder.addPropertyValue("maxConnections", element.getAttribute("maxConnections"));
    }

    private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
            ParserContext context) {

        AbstractBeanDefinition definition = builder.getBeanDefinition();
        definition.setSource(context.extractSource(source));
        return definition;
    }

}
